package development;

import java.util.Vector;

import state.Clip;
import state.GameAlert;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.component.GameScrollBar;
import state.component.MultiTab;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import state.util.ImageUtil;
import development.app.mxh.FrmAccount;
import development.bean.User;
import development.bean.UserBean;
import development.group.FrmGroupFriendAndTop;
import development.group.FrmGroupMenu;
import development.socket.SocketClientUtil;

public class FrmTopRiskest extends GameForm implements EventListener {
  private final int DISTANCE = 21;
  private final int DX = 10;
  private final int DY = 48;

  private ImageText text8;
  private int chooseIndex;

  private GameImage popupImage;
  
  private GameScrollBar scrollBar;
  private Clip textView; // Là khung nhìn để vẽ đoạn text ra
  private int textY;

  private static GameForm singleton;

  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmTopRiskest();
    }
    return singleton;
  }

  public void init(Hashtable parameters) {
    SocketClientUtil.requestTopRickest();
    popupImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "bg_popup.png");
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    _manager.enableTouchPad();
    textY = DY;
    textView = new Clip(DX, DY - 13, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT - DY - 30); // Là khung nhìn để vẽ đoạn text ra
    scrollBar = new GameScrollBar(Constants.SCREEN_WIDTH - 5, 25, 5, Constants.SCREEN_HEIGHT - 50, textView, _manager);
  }

  private void createMenu() {
    // Create bottom bar
    BottomBar bottomBar = DrawUtils.getBottomBar(_manager);
    bottomBar.setLabels("Menu", "", "Trở về");
    bottomBar.setLeftMenu("Menu", new GameMenu(null, new String[] { "Update danh sách", "Mời chơi", "Kết bạn", "Xem tài khoản" }, 4, this, Constants.MENU_STYLE, 0, GameConstants.SCREEN_HEIGHT - 45));
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
    if (Global.topRickest.size() == 0) {
      g.drawImage(popupImage, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
      text8.drawString2(g, "Đang cập nhật danh sách", GameColor.WHITE_CODE, GameConstants.SCREEN_WIDTH / 2, GameConstants.SCREEN_HEIGHT / 2, GameConstants.CENTER_ANCHOR);
    } else {
      Clip.setClip(g, textView);
      for (int i = 0; i < Global.topRickest.size(); i++) {
        g.setColor(GameColor.WHITE_CODE);
        g.drawLine(DX, textY - DISTANCE / 2 + i * DISTANCE, GameConstants.SCREEN_WIDTH - DX, textY - DISTANCE / 2 + i * DISTANCE);

        User user = (User) Global.topRickest.elementAt(i);
        int color = GameColor.WHITE_CODE;
        if (i == chooseIndex) {
          g.setColor(0x662221);
          g.fillRect(DX, textY - DISTANCE / 2 + i * DISTANCE + 2, GameConstants.SCREEN_WIDTH - 2 * DX, DISTANCE - 3);
          color = GameColor.YELLOW_CODE;
        }
        text8.drawString2(g, user.getName(), color, 26, textY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
        text8.drawString2(g, user.getMoney() + "$", color, GameConstants.SCREEN_WIDTH / 2 - 30, textY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
        text8.drawString2(g, user.getGold() + " gold", color, GameConstants.SCREEN_WIDTH / 2 + 30, textY + i * DISTANCE, GameConstants.VCENTER_LEFT_ANCHOR);
      }
      g.setClip(0, 0, Constants.SCREEN_WIDTH, Constants.SCREEN_HEIGHT);
      scrollBar.draw(g);
    }
  }

  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      createMenu();
      return;
    }

    // "Cập nhật danh sách", "Mời chơi", "Kết bạn"
    String action = event.getAction();
    if (action.equals("Update danh sách")) {
      SocketClientUtil.requestTopLevel();
    } else if (action.equals("Kết bạn")) {
      if (Global.topRickest.size() == 0) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy chọn một người chơi");
        return;
      }
      SocketClientUtil.addFriendRequest(((User) Global.topRickest.elementAt(chooseIndex)).getName());
    } else if (action.equals("Mời chơi")) {
      if (Global.topRickest.size() == 0) {
        Global.alert.showAlert(this, GameAlert.OK_TYPE, "Hãy chọn một người chơi");
        return;
      }
      Vector choosedUsers = new Vector();
      choosedUsers.addElement(((User) Global.topRickest.elementAt(chooseIndex)).getName());
      SocketClientUtil.invitePlay(choosedUsers, Global.currentGame.getId());
    } else if (action.equals("Xem tài khoản")) {
      UserBean friend = (UserBean) Global.friends.elementAt(chooseIndex);
      Hashtable parameters = new Hashtable();
      parameters.put("User", friend.getUsername());
      FrmAccount.backForm = FrmGroupFriendAndTop.instance();
      Global.nextState(FrmAccount.instance(), parameters);
    }
  }

  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.SOFT_RIGHT:
      if (parentGroup instanceof FrmGroupFriendAndTop) {
        if (FrmGroupFriendAndTop.backForm != null){
          Global.nextState(FrmGroupFriendAndTop.backForm, FrmGroupFriendAndTop.parameters);
        } else {
          if (Global.systemCanvas.lastState instanceof FrmDownloadResource) {
            GameGlobal.nextState(FrmGroupMenu.instance(), null);
          } else {
            GameGlobal.nextState(Global.systemCanvas.lastState, FrmGroupFriendAndTop.parameters);
          }
        }
      }
      break;
    case Key.UP:
    case Key.K_2:
      if (chooseIndex > 0) {
        chooseIndex--;
        if (textY + chooseIndex * DISTANCE < DY) {
          textY += DISTANCE;
        }
      }
      break;
    case Key.DOWN:
    case Key.K_8:
      if (chooseIndex < Global.topRickest.size() - 1) {
        chooseIndex++;
        if (textY + chooseIndex * DISTANCE > Constants.SCREEN_HEIGHT / 2) {
          textY -= DISTANCE;
        }
      }
      break;
    }
  }

  public void destroy() {
    popupImage = null;
    text8 = null;
  }
}
